using System;
using System.Collections;
using System.Collections.Generic;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using com.yoozoo.gta.Tools.UI;
using UnityEngine;
using UnityEngine.UI;

namespace com.yoozoo.gta
{
    [ExecuteAlways]
    public class FullImageScaler : ScreenOrientationChangeHandler
    {
        public override int Order => (int)AdapterExecuteOrder.YImageScaler;
        public bool SupportPortrait = true;
        private bool isSetWH;

        private Vector3 _originScale = Vector3.zero;

        private ScreenOrientation currentOrientation;

        // Start is called before the first frame update
        void Start()
        {
            _originScale = this.transform.localScale;
            currentOrientation = ScreenOrientManager.GetInstance().CurrentLogicOrientation;
        }
        private void OnEnable()
        {
            isSetWH = false;

        }
#if UNITY_EDITOR
        private void OnValidate()
        {
            // isSetWH = false;
            // Update();
        }
#endif
        // Update is called once per frame
        void Update()
        {
            if (!SupportPortrait && currentOrientation == ScreenOrientation.Portrait)
            {
                return;
            }

            if (!isSetWH)
            {
               
                var rectTransform = (RectTransform)this.transform; 
                var parentRectTransform = (RectTransform)rectTransform.parent;
                var canvasScaler = this.transform.GetComponentInParent<CanvasScaler>();
                if (canvasScaler != null)
                {
                    parentRectTransform = canvasScaler.GetComponent<RectTransform>();
                }
               
                if(parentRectTransform== null)
                {
                    return;
                }
      
                var parentRect = parentRectTransform.rect;

                
                if (parentRect.width > 0)
                {
                   
                    var currentRect = rectTransform.rect;
                    if (parentRect.width > currentRect.width || parentRect.height > currentRect.height)
                    {
                        var currentWH = currentRect.width / currentRect.height;
                        var parentWH = parentRect.width / parentRect.height;
                        if (parentWH > currentWH)
                        {
                            //rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,100);
                            //rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical,100/currentWH);
                            float scale = parentRect.width / currentRect.width;
                            rectTransform.localScale = new Vector3(scale,scale,1);
                        }
                        else
                        {
                            //rectTransform.sizeDelta = new Vector2(parentRectTransform.sizeDelta.x*100*currentWH,parentRectTransform.sizeDelta.y);
                            float scale = parentRect.height/currentRect.height;
                            rectTransform.localScale = new Vector3(scale,scale,1);
                        }
                    }
                    else if(_originScale != Vector3.zero)
                    {
                        rectTransform.localScale = _originScale;
                    }
                 

                    isSetWH = true;
                }
              
            }
        }
        public void Awake()
        {
            Debug.Log("Awake..............");
            ScreenOrientManager.GetInstance().AddOrientationChangeListener(this);
            
        }
        public void OnDestroy()
        {
            Debug.Log("OnDestroy..............");
            ScreenOrientManager.GetInstance().RemoveOrientationChangeListener(this);
        }
        public override void ScreenOrientationChange(ScreenOrientation orientation, bool needForceApply = false)
        {
            currentOrientation = orientation;
            if (!SupportPortrait && ScreenOrientManager.GetInstance().IsPortrait())
            {
                return;
            }

            
            isSetWH = false;
        }
    }
}
